package com.lh.singleton.type1;


//有参构造
public class Singleton2 {
	public static void main(String[] args) {

		Singleton02 instance = Singleton02.getInstance("张三",23);
		Singleton02 instance2 = Singleton02.getInstance("李四",23);
		String name = instance2.getName();
		int age = instance2.getAge();
		System.out.println(name +" "+ age);

		System.out.println(instance.hashCode() == instance2.hashCode());

		System.out.println(instance2);
	}
}

class Singleton02 {

	// 1.构造器私有化，外部能new
	private static Singleton02 instance;

	private String name;
	private int age;

	//无参构造
	private Singleton02() {

	}

	// 开始有参构造
	private Singleton02(String name,int age) {
		this.name = name;
		this.age = age;
	}

	// 提供一个公共的静态方法，返回实例对象
	public static Singleton02 getInstance() {
		if (instance == null) {
			instance = new Singleton02();
		}
		return instance;
	}


	// 提供一个公共的静态方法，返回实例对象
	public static Singleton02 getInstance(String name,int age) {

		if (instance == null) {
			instance = new Singleton02(name, age);

		}

		return instance;
	}

	public void show() { System.out.println("测试一下"); }

	public String getName() { return name; }

	public void setName(String name) { this.name = name; }

	public int getAge() { return age; }

	public void setAge(int age) { this.age = age; }

	@Override
	public String toString() {
		return "name=" + name + ", age=" + age;
	}


}